/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Pojo.CostPojo;
import Pojo.PeerPojo;
import define.Define;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.Security;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

/**
 *
 * @author Xuan Cau
 */
public class Utils {
    public static PeerPojo []arr_peer;
    public static CostPojo []arr_cost;
    public static PeerPojo []peer_online;
    public static int port;
    public static DatagramSocket socket;
    static public int RINT8(ByteBuffer buf)
    {
        return buf.get();
    }
    static public int RINT16(ByteBuffer buf)
    {
        return buf.getShort();
    }
    static public int RINT32(ByteBuffer buf)
    {
        return buf.getInt();
    }
    static public int bufbsize(byte[] buf)
    {
	int size = Define.SIZE_OF_INT8;
	if(buf != null)
		size += buf.length;
	return size;
    }

    static public int bufssize(byte[] buf)
    {
	int size = Define.SIZE_OF_INT16;
	if(buf != null)
            size += buf.length;
	return size;
    }
    static public final ByteBuffer wBBin(ByteBuffer buf, byte[] bin)
    {
	if(bin != null)
	{
            buf.put((byte)(bin.length));
            return buf.put(bin);
	}
	return  buf.put((byte)(0));
    }
    static public final ByteBuffer wSBin(ByteBuffer buf, byte[] bin)
    {
	if(bin != null)
	{
            buf.putShort((short)(bin.length));
            return buf.put(bin);
	}
	return  buf.putShort((short)(0));
    }
    static public final ByteBuffer ALLOCATE(int reqlen)
    {
       return  ByteBuffer.allocate(reqlen);
    }
    static public final byte[] rBin(ByteBuffer buf, int len)
    {
	byte[] data = null;
	if(len > 0)
	{
            data = new byte[len];
            buf.get(data);
	}
	return data;
    }
    static public final byte[] rBBin(ByteBuffer buf)
    {
	return rBin(buf, buf.get()&0xFF);
    }
	
    static public final byte[] rSBin(ByteBuffer buf)
    {
	return rBin(buf, buf.getShort()&0xFFFF);
    }
    public static byte[] SHA1(byte[] input)
    {
        try
        {
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            MessageDigest sha = MessageDigest.getInstance("SHA-1");

            sha.update(input);
            byte[] msgDigest = sha.digest();
            
            return msgDigest;
        }
        catch(Exception ex)
        {
            return null;
        }
    }
    public static final byte[] intToByteArray(int value) 
    {
        return new byte[] {
            (byte)(value >>> 24),
            (byte)(value >>> 16),
            (byte)(value >>> 8),
            (byte)value};
    }
    public static final String GetHostName()
    {
        String host_name = null;
        try
        {
            return host_name = InetAddress.getLocalHost().getHostName();
        }
        catch(Exception ex)
        {
        }
        return host_name;
    }
    
    public static String GetIP()
    {
        String ip = null;
        try
        {
            return ip = String.valueOf(InetAddress.getLocalHost().getHostAddress());
        }
        catch(Exception ex)
        {
        }
        return ip;
    }
   
    public static final byte[] GetHostAddrees()
    {
        try
        {
            return InetAddress.getLocalHost().getAddress();
        }
        catch(Exception ex)
        {
        }
        return null;
    }
    public static final String Remove(String str,String ex)
    {
       return str.substring(0,str.length() - ex.length());
    }

    public static String getFileName(String filename) {
        String name = "";
        int index = filename.lastIndexOf("\\");
        if (index != -1) {
            name = filename.substring(index + 1, filename.length());
        }
        return name;
    }

    public static String getFilePath(String filename) {
        String path = "";
        int index = filename.lastIndexOf("\\");
        if (index != -1) {
            path = filename.substring(0, index);
        }
        return path;
    }
    public static String backslashReplace(String myStr) {
        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator(myStr);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            if (character == '\\') {
                result.append("/");
            } else {
                result.append(character);
            }
            character = iterator.next();
        }
        return result.toString();
    }
    
    public static void ReadFileConfig()
    {
        try {
            
            FileInputStream fis = new FileInputStream("Config\\Nodes.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));      
            int nodes = Integer.parseInt(br.readLine());
            arr_peer = new PeerPojo[nodes];
            for(int i =0; i< nodes; i++)
            {
                PeerPojo peer = new PeerPojo();
                String str = br.readLine();
                String []str_tp = str.split(" ");
                peer.setID(Integer.parseInt(str_tp[0]));
                peer.setIp((str_tp[1]));
                peer.setPort(Integer.parseInt(str_tp[2]));
                arr_peer[i] = peer;
            }
            int m = Integer.parseInt(br.readLine());
            arr_cost = new CostPojo[m];
            for(int i =0; i< m; i++)
            {
                CostPojo cost = new CostPojo();
                String str = br.readLine();
                
                String []str_tp = str.split(" ");
                cost.setPeer1(Integer.parseInt(str_tp[0]));
                cost.setPeer2(Integer.parseInt(str_tp[1]));
                cost.setCost(Integer.parseInt(str_tp[2]));
                arr_cost[i] = cost;
            }
            
            //return arr_peer;
            /*for(int i=0; i< arr_peer.length ; i++)
            {
                PeerPojo peer = new PeerPojo();
                peer = arr_peer[i];
                System.out.println("id :" + peer.getID());
                System.out.println("ip :" + peer.getIp());
                System.out.println("port :" + peer.getPort());
            }*/
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        
    }

    public static final long Seconds()
    {
        return System.currentTimeMillis()/1000;
    }
    public static final byte[][] SplitPackage(byte[] data)
    {
        int num_pack = data.length / Define.MAX_LEN;
        return null;
    }
}
